package net.xumf.domain.master;

import com.alibaba.fastjson.annotation.JSONField;
import lombok.Data;
import org.hibernate.annotations.ColumnDefault;
import org.hibernate.annotations.Generated;
import org.hibernate.annotations.GenerationTime;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;

@Data
@Entity
@Table(name = "system_permission")
@NamedEntityGraph(name = "SystemPermission.module",
        attributeNodes = @NamedAttributeNode("module"))
public class SystemPermission implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "permission_id")
    private Integer permissionId;

    // 权限名称
    @Column(name = "name", length = 50, nullable = false)
    private String name;

    // 权限代号
    @Column(name = "code", length = 50, nullable = false, unique = true)
    private String code;

    // 系统模块标识
    @Column(name = "module_id", nullable = false)
    private Integer moduleId;

    // 系统模块
    @JSONField(serialize = false)
    @OneToOne(targetEntity = SystemModule.class, cascade = CascadeType.DETACH, fetch = FetchType.LAZY)
    @JoinColumn(name = "module_id", referencedColumnName = "module_id", insertable = false, updatable = false)
    private SystemModule module;

    // 排序
    @Column(name = "sequence", nullable = false)
    @ColumnDefault("1")
    private Integer sequence;

    // 系统操作备注
    @Column(name = "comment", length = 50, nullable = false)
    private String comment;

    // 创建时间
    @Column(name = "created_at", nullable = false, insertable = false, updatable = false, columnDefinition = "timestamp default current_timestamp")
    @Temporal(TemporalType.DATE)
    @Generated(GenerationTime.INSERT)
    private Date createdAt;

    // 更新时间
    @Column(name = "updated_at", nullable = false, insertable = false, columnDefinition = "timestamp default current_timestamp")
    @Temporal(TemporalType.DATE)
    @Generated(GenerationTime.INSERT)
    private Date updatedAt;
}

